<template>
  <div class="app-container">
    <div style="text-align: center;margin-bottom: 30px">
      视频设备{{ createStatus ? '新增' : '更新' }}
    </div>
    <el-row :gutter="10">
      <el-form :model="cameraForm" ref="cameraForm" :rules="cameraFormRules" label-width="100px">
          <el-form-item label="安全预警标题" prop="title">
            <el-input type="text" v-model="cameraForm.title" placeholder="请输入安全预警标题"
                      clearable></el-input>
          </el-form-item>
      </el-form>
    </el-row>


    <div slot="footer" style="text-align: center" class="dialog-footer">
      <el-button @click="cancelData">取 消</el-button>
      <el-button :loading="addLoading" v-if="createStatus" type="primary" @click="createData">创 建
      </el-button>
      <el-button :loading="addLoading" v-else type="primary" @click="updateData">修 改</el-button>
    </div>
  </div>
</template>

<script>
import {getData, addData, updateData} from "@/api/warning";
import {getToken} from "@/utils/auth";

export default {
  name: "dataEdit",
  components: {},
  data() {
    return {
      cameraForm: {
        name: "",
        regionId: "",
        code: "",
        ip: ""
      },
      cameraFormRules: {
        title: [{required: true, message: '设备名称不能为空', trigger: 'blur'}]
      },
      addLoading: false,
      regionList: []
    }
  },
  computed: {
    createStatus() {
      return this.$route.params.ucId == '0'
    },
    headers() {
      return {
        'Authorization': getToken()
      }
    }
  },
  created() {
    this.getRegionList()
    if (!this.createStatus) {
      let ucId = this.$route.params.ucId
      this.getData(ucId)
    }
  },
  methods: {
    getData(val) {
      getData({ucId: val}).then(res => {
        this.cameraForm = res.data
      }).catch(res => {

      })
    },
    cancelData() {
      window.history.back()
    },
    createData() {
      this.addLoading = true
      this.$refs['cameraForm'].validate(valid => {
        if (valid) {
          console.log(this.cameraForm)
          addData(this.cameraForm).then(res => {
            this.addLoading = false
            this.$notify.success({
              title: '成功',
              message: "添加视频设备成功"
            })
            this.cancelData()
          }).catch(res => {
            this.addLoading = false
            this.$notify.error({
              title: '失败',
              message: res.msg
            })
          })
        } else {
          this.addLoading = false
          console.log("表单验证不通过")
        }
      })
    },
    updateData() {
      this.addLoading = true
      this.$refs['cameraForm'].validate(valid => {
        if (valid) {
          updateData(this.cameraForm).then(res => {
            this.addLoading = false
            this.$notify.success({
              title: '成功',
              message: "更新视频设备成功"
            })
            this.cancelData()
          }).catch(res => {
            this.addLoading = false
            this.$notify.error({
              title: '失败',
              message: res.msg
            })
          })
        } else {
          this.addLoading = false
          console.log("表单验证不通过")
        }
      })
    },
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 149px;
  height: 149px;
  line-height: 149px;
  text-align: center;
}

.avatar {
  width: 149px;
  height: 149px;
  display: block;
}
</style>
